package pri.damai.xiaowu.order.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pri.damai.xiaowu.goods.common.command.StockReduceCmd;
import pri.damai.xiaowu.order.common.dto.CreateOrderDTO;
import pri.damai.xiaowu.order.server.config.IdUtil;
import pri.damai.xiaowu.order.server.dao.OdrOrderDao;
import pri.damai.xiaowu.order.server.entity.OdrOrder;
import pri.damai.xiaowu.order.server.entity.OdrOrderGoods;
import pri.damai.xiaowu.order.server.manager.GoodsManager;
import pri.damai.xiaowu.order.server.service.OdrOrderService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单表(OdrOrder)表服务实现类
 *
 * @author makejava
 * @since 2022-04-28 21:39:45
 */
@Service("odrOrderService")
public class OdrOrderServiceImpl extends ServiceImpl<OdrOrderDao, OdrOrder> implements OdrOrderService {

    @Resource
    OdrOrderGoodsServiceImpl orderGoodsService;
    @Resource
    OdrOrderDao orderDao;
    @Resource
    GoodsManager goodsManager;

    @Transactional(rollbackFor = Exception.class)
    public void createOrder(CreateOrderDTO createOrderDTO) {

        OdrOrder odrOrder = new OdrOrder();
        odrOrder.setMerchantId(1L);
        odrOrder.setUserId(createOrderDTO.getUserId());

        //商品总金额
        Long goodsTotalAmount = 0L;
        //运费总金额
        Long freightTotalAmount = 0L;
        //折扣总金额
        Long discountTotalAmount = 0L;
        //支付总金额
        Long payTotalAmount = 0L;

        List<OdrOrderGoods> odrOrderGoodsList = new ArrayList<>();
        List<StockReduceCmd> stockReduceCmds = new ArrayList<>();

        for (CreateOrderDTO.OrderGoods orderGoods : createOrderDTO.getGoodsList()) {
            OdrOrderGoods odrOrderGoods = new OdrOrderGoods();
            BeanUtils.copyProperties(orderGoods, odrOrderGoods);
            odrOrderGoodsList.add(odrOrderGoods);

            goodsTotalAmount = goodsTotalAmount + (odrOrderGoods.getSellingPrice() * odrOrderGoods.getGoodsNum());
            payTotalAmount = payTotalAmount + (odrOrderGoods.getSettlementPrice() * odrOrderGoods.getGoodsNum());

            StockReduceCmd reduceCmd = new StockReduceCmd();
            reduceCmd.setSkuId(orderGoods.getSkuId());
            reduceCmd.setReduceNum(orderGoods.getGoodsNum());
            stockReduceCmds.add(reduceCmd);
        }

        odrOrder.setGoodsTotalAmount(goodsTotalAmount);
        odrOrder.setFreightTotalAmount(freightTotalAmount);
        odrOrder.setDiscountTotalAmount(discountTotalAmount);
        odrOrder.setPayTotalAmount(payTotalAmount);
        odrOrder.setOrderNo(IdUtil.getOrderNo("1", odrOrder.getMerchantId() + ""));

        orderDao.insert(odrOrder);

        odrOrderGoodsList.forEach(s -> {
            s.setOrderId(odrOrder.getId());
        });

        orderGoodsService.saveBatch(odrOrderGoodsList);

//        goodsManager.reduceStockNum(stockReduceCmds);
    }

}

